﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoCadConvert
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class LoginControl : System.Windows.Window
    {
        private string m_sCommand = "";
        public bool m_AutoLogin = false;
        public LoginControl(string sCommand)
        {
            m_sCommand = sCommand;
            InitializeComponent();
            new WindowInteropHelper(this).Owner = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Window.Handle;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        }
        private void Image_MouseDown_Close(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
        private void Image_MouseEnter_Close(object sender, MouseEventArgs e)
        {
            (sender as Image).Source = new BitmapImage(new Uri(@"/AutoCadConvert;component/Image/1@2x.png",UriKind.Relative));
        }
        private void Image_MouseLeave_Leave(object sender, MouseEventArgs e)
        {
            (sender as Image).Source = new BitmapImage(new Uri(@"/AutoCadConvert;component/Image/Close@2x.png", UriKind.Relative));
        }
        private void Window_MouseMove_Window(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && e.Source == sender)
            {
                this.DragMove();
            }
        }
        private void Image_MouseDown_Radio(object sender, MouseButtonEventArgs e)
        {
            m_AutoLogin = !m_AutoLogin;
            if (m_AutoLogin)
                (sender as Image).Source = new BitmapImage(new Uri(@"/AutoCadConvert;component/Image/选中@2x.png", UriKind.Relative));
            else
                (sender as Image).Source = new BitmapImage(new Uri(@"/AutoCadConvert;component/Image/未选@2x.png", UriKind.Relative));
        }
        private void Label_MouseDown_ForgetPassWord(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "https://www.sz-swjx.cn/";
            proc.Start();
        }

        private void Label_MouseEnter_ForgetPassWord(object sender, MouseEventArgs e)
        {
            (sender as Label).Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(32,184,226));
        }

        private void Label_MouseLeave_ForgetPassWord(object sender, MouseEventArgs e)
        {
            (sender as Label).Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(153,153,153));
        }

        private void Button_Click_Login(object sender, RoutedEventArgs e)
        {//登陆
            string retString = "";
            if(LoginInfo.instance().Login(userName.Text, passWord.Password,ref retString))
            {
                this.SaveFile();
                this.Close();
                if (m_sCommand == "ExportBSPXml")
                    DwgConvert.ExportBSPXml();
                else if (m_sCommand == "CreateDefaultLayer")
                    DwgConvert.CreateDefaultLayer();
                else
                    DwgConvert.PaletteSetPopOut();//显示任务窗口     
            }
            else
            {
                tip.Content = retString;
                new Timer(new TimerCallback(o =>
                {//取消错误提示
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        tip.Content = "";
                    }));
                }), this, 2000, 0);
            }
        }

        private void SaveFile()
        {
            //自动登陆
            try
            {
                if (m_AutoLogin == true)
                {
                    string content = LoginInfo.instance().userName + "\n" 
                        + LoginInfo.instance().userNameBase64 + "\n" 
                        + LoginInfo.instance().passwordMD5;
                    File.WriteAllText(LoginInfo.instance().globalfileName, content);
                }
                else
                {
                    File.Delete(LoginInfo.instance().globalfileName);
                }
            }
            catch (Exception ex)
            {
                DwgConvert.PutMessage(ex.Message);
            }
        }
    }
}
